/*!
 * Ext JS Library 3.3.1
 * Copyright(c) 2006-2010 Sencha Inc.
 * licensing@sencha.com
 * http://www.sencha.com/license
 */
ShareRequestWindow = Ext.extend(Ext.app.Module, {
    id: 'sharereq-win',
    autostart: true,
    store: null,
    init: function() {
        this.launcher = {
            text: 'Incoming Share Requests',
            iconCls: 'refwin-icon',
            handler: this.createWindow,
            scope: this
        };
    },
    
    checkForUpdate: function() {
    	var ref = this;
    	window.setTimeout(function() {
    		ref.store.reload();
    		ref.checkForUpdate();
    	}, 5000);
    },

     createWindow: function() {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('sharereq-win');
        
        // create the data store
        var store = new Ext.data.JsonStore({
        	autoDestroy: true,
        	totalProperty: 'results',
        	root: 'rows',
        	url: './references?action=getShareRequests',
        	fields: ['id', 'ownerUsername', 'sourceUri', 'sourceXpath', 'targetUri', 'targetXpath']
        });
        store.on('load', function() {
        	if (store.getTotalCount() > 0) {
        		win.show();
        	} else {
        		win.hide();
        	}
        });
        this.store = store;

        // create the Grid
        var grid = new Ext.grid.GridPanel({
            store: store,
            columns: [
				{
				    id       :'id',
				    hidden	 : true,
				    header   : 'ID',
				    width    : 80,
				    sortable : true,
				    dataIndex: 'id'
				},
				{
				    id       :'ownerUsername',
				    header   : 'User',
				    width    : 80,
				    sortable : true,
				    dataIndex: 'ownerUsername'
				},
                {
                    id       :'sourceUri',
                    header   : 'Source URI',
                    width    : 80,
                    sortable : true,
                    dataIndex: 'sourceUri'
                },
                {
                    id       :'sourceXpath',
                    header   : 'Source XPath',
                    width    : 80,
                    sortable : true,
                    dataIndex: 'sourceXpath'
                },
                {
                    id		 : 'targetUri',
                    header   : 'Target URI',
                    width    : 170,
                    sortable : true,
                    dataIndex: 'targetUri' 
                },
                {
                    id       :'targetXpath',
                    header   : 'Target XPath',
                    width    : 80,
                    sortable : true,
                    dataIndex: 'targetXpath'
                }
            ],
            stripeRows: true,
            title: 'Incoming Share Requests',
            stateful: true,
            stateId: 'grid'
        });
        grid.addListener('rowcontextmenu', function(grid, rowIndex, e) {
        	e.stopEvent(); 
        	grid.getSelectionModel().selectRow(rowIndex); 
        	var row = grid.getStore().getAt(rowIndex);
        	var coords = e.getXY(); 
        	var items = new Array();
        	var id = row.data.id;
        	var sourceUri = row.data.sourceUri;
        	var sourceXpath = row.data.sourceXpath;
        	var targetUri = row.data.targetUri;
        	var targetXpath = row.data.targetXpath;
        	
        	items.push({text: 'Preview Source', handler: function() {
        		var title = sourceUri.substr(sourceUri.indexOf("://")+3);
            	title = title.substr(0, title.indexOf("/"));
            	MyDesktop.getModule('browser-win').createWindow(title, sourceUri, sourceXpath);
        	}});
        	items.push({text: 'Preview Target', handler: function() {
        		var title = targetUri.substr(targetUri.indexOf("://")+3);
            	title = title.substr(0, title.indexOf("/"));
            	MyDesktop.getModule('browser-win').createWindow(title, targetUri, targetXpath);
        	}});
			items.push({text: 'Accept', handler: function() {
				ReferenceManager.respondToShareRequests(id, true, function() {
        			store.reload();
        		});
        	}});
        	items.push({text: 'Reject', handler: function() {
        		ReferenceManager.respondToShareRequests(id, false, function() {
        			store.reload();
        		});
        	}});
        	var menu = new Ext.menu.Menu({
                items: items
            });
            menu.showAt([coords[0], coords[1]]); 
        });
        store.load();
        
        if (!win) {
            win = desktop.createWindow({
                id: 'sharereq-win',
                title: 'Incoming Share Requests',
                width: 500,
                height: 250,
                x: 340,
                y: 400,
                iconCls: 'refwin-icon',
                shim: false,
                animCollapse: false,
                border: false,
                constrainHeader: true,
                collapsible: true,
                layout: 'fit',
                items: [ grid ]
            });
        }
        //win.show();
        this.checkForUpdate();
     }
});